package com.yxw.yx_hotel.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yx_hotel.dto.AddRegionDto;
import com.yxw.yx_hotel.dto.QueryRegionDto;
import com.yxw.yx_hotel.dto.UpdateRegionDto;
import com.yxw.yx_hotel.entity.YxHotelRegionEntity;
import com.yxw.yx_hotel.entity.YxRoomEntity;
import com.yxw.yx_hotel.mapper.RegionMapper;
import com.yxw.yx_hotel.mapper.RoomMapper;
import com.yxw.yx_hotel.service.RegionService;
import com.yxw.yx_hotel.utils.PageUtils;
import com.yxw.yx_hotel.utils.UserUtil;
import com.yxw.yx_hotel.vo.UserTokenVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/*
import java.util.ArrayList;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;
*/
/**
 *  区域管理配置
 * @author liuxin
 * @date 2024.01.17
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, YxHotelRegionEntity> implements RegionService {

    @Autowired
    private UserUtil userUtil;
    @Autowired
    private RoomMapper roomMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRegion(AddRegionDto dto) {
        UserTokenVo loginUserVo = userUtil.getLoginUserVo();
        if(null==loginUserVo){
            throw new BaseException("未获取到操作人信息");
        }
        YxHotelRegionEntity entity = new YxHotelRegionEntity();
        entity.setRegionName(dto.getRegionName().trim());
        entity.setStatus(1);
        entity.setCreateId(loginUserVo.getUserId());
        entity.setCreateName(loginUserVo.getUserName());
        entity.setTenantId(loginUserVo.getTenantId());
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
    }

    /**
     * 查询区域配置列表
     * @param dto
     * @return
     */
    @Override
    public PageVO pageRegionList(QueryRegionDto dto) {
        //List<RegionVo> voList=new ArrayList<>();
        Long tenantId = userUtil.getLoginTenantId();
        Page<YxHotelRegionEntity> page = new Page<>(dto.getCurrPage(),dto.getPageSize());
        QueryWrapper<YxHotelRegionEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("tenant_id",tenantId);
        if(StringUtils.isNotBlank(dto.getRegionName())){
            queryWrapper.like("regin_name",dto.getRegionName().trim());
        }
        queryWrapper.orderByDesc("create_time");
        Page<YxHotelRegionEntity> entityPage = baseMapper.selectPage(page, queryWrapper);
        return PageUtils.get(entityPage);
        //List<YxHotelRegionEntity> records = entityPage.getRecords();
        //if(null!=records && !records.isEmpty()){
        //    voList=BeanUtil.copyToList(records, RegionVo.class);
        //}
        //return PageUtils.get(page, voList);
    }
    /**
     * 查询区域配置列表
     * @param dto
     * @return
     */
    @Override
    public List getRegionList(String regionName)
    {
        Long tenantId = userUtil.getLoginTenantId();      
        QueryWrapper<YxHotelRegionEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("tenant_id",tenantId);
        if(StringUtils.isNotBlank(regionName)){
            queryWrapper.like("regin_name",regionName.trim());
        }
        return baseMapper.selectList(queryWrapper);
    }
    /**
     * 删除区域信息
     * @param regionId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRegionById(Long regionId) {
        UserTokenVo loginUserVo = userUtil.getLoginUserVo();
        YxHotelRegionEntity entity = baseMapper.selectById(regionId);
        if(null==entity){
            throw new BaseException("未找到需要删除的区域信息");
        }
        if(!loginUserVo.getTenantId().equals(entity.getTenantId()))
        {
            throw new BaseException("没有删除该区域的权限");
        }
        QueryWrapper<YxRoomEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("region_id", regionId);
        Integer iCounter = roomMapper.selectCount(queryWrapper);
        if(null == iCounter)
        {
            iCounter = 0;
        }
        if(iCounter == 0)
        {
            int cnt = baseMapper.deleteById(regionId);
            if(0==cnt){
                throw new BaseException("删除失败,未找到需要删除的区域信息");
            }
        }
        else
        {
            throw new BaseException("删除失败,已经在该区域添加了宿舍");
        }
    }

    /**
     * 修改区域信息
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRegionById(UpdateRegionDto dto) {
        UserTokenVo loginUserVo = userUtil.getLoginUserVo();
        YxHotelRegionEntity entity = baseMapper.selectById(dto.getRegionId());
        if(null==entity){
            throw new BaseException("未找到需要修改的区域信息");
        }
        if(!loginUserVo.getTenantId().equals(entity.getTenantId()))
        {
            throw new BaseException("没有修改该区域信息的权限");
        }
        if(!entity.getRegionName().equals(dto.getRegionName().trim()))
        {
            entity.setRegionName(dto.getRegionName());
            entity.setUpdateId(loginUserVo.getUserId());
            entity.setUpdateName(loginUserVo.getNickName());
            entity.setUpdateTime(new Date());
            UpdateWrapper<YxRoomEntity> updateWrapper = new UpdateWrapper();
            updateWrapper.eq("region_id", dto.getRegionId());
            updateWrapper.eq("tenant_id", loginUserVo.getTenantId());
            updateWrapper.set("region_name", dto.getRegionName());
            baseMapper.updateById(entity);
            roomMapper.update(null, updateWrapper);
        }
    }
}
